const Mock = require('mockjs')
const List = []
const count = 20

// 拓展mockjs
Mock.Random.extend({
  phone: function() {
    var phonePrefixs = ['132', '135', '189'] // 自己写前缀哈
    return this.pick(phonePrefixs) + Mock.mock(/\d{8}/) // Number()
  }
})
for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    userId: '@increment',
    careteDate: '@date(yyyy-MM-dd hh:mm:ss )',
    userName: '@first',
    nickName: '@first',
    phonenumber: '@phone',
    status: '@integer(0,1)'
  }))
}

module.exports = [
  // 用户列表分页
  {
    url: '/system/user/list',
    type: 'get',
    response: config => {
      const { userName, phonenumber, status, page = 1, size = 20 } = config.query
      const mockList = List.filter(item => {
        if (userName && item.userName !== userName) return false
        if (phonenumber && item.phonenumber !== phonenumber) return false
        if (status && item.status !== status) return false
        return true
      })
      const pageList = mockList.filter((item, index) => index < size * page && index >= size * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  // 获取用户数据
  {
    url: '/system/user',
    type: 'get',
    response: config => {
      // const { userId } = config.query
      for (const user of List) {
        return {
          code: 20000,
          roles: [{ 'roleId': '1', 'roleName': '管理员' }, { 'roleId': '2', 'roleName': '普通员工' }],
          data: user,
          roleIds: '1'
        }
      }
    }
  },
  // 新增用户
  {
    url: '/system/user',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  // 修改用户
  {
    url: '/system/user/update',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  // 删除用户
  {
    url: '/system/user/delete',
    type: 'delete',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]
